#!/bin/bash
# Copyright (C) 2024 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

unset LANG

# shellcheck source=/dev/null
. "${OMD_ROOT}/etc/omd/site.conf"
if [ "${CONFIG_AUTOMATION_HELPER}" != "on" ]; then
    exit 5
fi

PIDFILE=$OMD_ROOT/tmp/run/automation-helper.pid
DAEMON=$OMD_ROOT/bin/cmk-automation-helper
THE_PID=$(cat "$PIDFILE" 2>/dev/null)

process_is_running() {
    [ -e "$PIDFILE" ] && kill -0 "$THE_PID" 2>/dev/null
}

await_process_stop() {
    max=$(("${1}" * 10)) # tenths of a second
    for N in $(seq "${max}"); do
        process_is_running || return 0
        [ $((N % 10)) -eq 0 ] && printf "."
        sleep 0.1
    done
    return 1
}

force_kill() {
    printf 'sending SIGKILL.'
    kill -9 "${THE_PID}"
}

exit_successfully() {
    printf "%s\n" "${1}"
    exit 0
}

exit_failure() {
    printf "%s\n" "${1}"
    exit 1
}

case "$1" in
    start)
        echo -n 'Starting automation-helper...'
        # shellcheck disable=SC2086 # Yes, we want splitting for OPTS.
        if process_is_running; then
            exit_successfully 'already running.'
        fi

        if "$DAEMON"; then
            exit_successfully 'OK'
        else
            exit_failure 'failed'
        fi
        ;;
    stop)
        echo -n 'Stopping automation-helper...'
        if [ -z "$THE_PID" ]; then
            exit_successfully 'not running.'
        fi

        if ! process_is_running; then
            exit_successfully 'not running (PID file orphaned)'
        fi

        echo -n "killing $THE_PID..."
        if ! kill "$THE_PID" 2>/dev/null; then
            exit_successfully 'OK'
        fi

        # Signal could be sent

        # Patiently wait for the process to stop
        if await_process_stop 60; then
            exit_successfully 'OK'
        fi

        # Insist on killing the process
        force_kill
        if await_process_stop 10; then
            exit_successfully 'OK'
        fi
        exit_failure 'failed'
        ;;

    restart | reload)
        $0 stop && $0 start
        ;;

    status)
        echo -n 'Checking status of automation-helper...'
        if [ -z "$THE_PID" ]; then
            exit_failure 'not running (PID file missing)'
        fi

        if ! process_is_running; then
            exit_failure 'not running (PID file orphaned)'
        fi
        exit_successfully 'running'
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|status|flush}"
        ;;
esac
